﻿//======================================================================
//
//        Copyright (C) 2008 HOHO``
//        All rights reserved
//
//        filename :$safeitemrootname$
//        description :作业中心操作类.
//
//        created by 裘中杰 at  $time$
//======================================================================


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using System.Web;
using System.Web.UI;
using Hisun.Models;
using System.Data.Linq;

namespace Hisun.Controllers
{
    public class AbcCostCenter
    {
        protected HisunDBDataContext db = new HisunDBDataContext();
        protected string lastError;

        virtual public string LastError
        {
            get { return lastError; }
            set { lastError = value; }
        }

        /// <summary>
        /// 虚拟 内部嵌套
        /// </summary>
        /// <param name="menuID"></param>
        /// <param name="totalMenus"></param>
        /// <param name="tn"></param>
        virtual protected void FillData2TreeView(int menuID, IEnumerable<TABC_JS_CostCenter> totalMenus, ref TreeNode tn)
        {
            var menus = (from m in totalMenus
                         where m.上级作业中心ID == menuID
                         orderby m.作业中心ID ascending
                         select m);
            if (menus.Count() == 0)
                return;
            foreach (var menu in menus)
            {
                if (menu.作业中心ID == menu.上级作业中心ID)
                    throw new Exception(string.Format("严重错误 ！作业中心ID中条目[{0}]重复与上级作业中心ID！", menu.作业中心名称));
                TreeNode childTN = new TreeNode(menu.作业中心名称, menu.作业中心ID.ToString(), @"\Skins\Default\Menu\folder.gif", string.Format("Javascript:ClickMenu('{0}','{1}');", menu.作业中心名称, menu.作业中心ID), "_self");
                if (menu.停用.GetValueOrDefault(true))
                    childTN.Text = string.Format(@"<font color=""red"">{0}</font>", childTN.Text);
                FillData2TreeView(menu.作业中心ID, totalMenus, ref childTN);
                tn.ChildNodes.Add(childTN);
            }
        }


        /// <summary>
        /// 获得菜单
        /// </summary>
        /// <param name="startInt">开始符号</param>
        /// <param name="totalMenu">菜单数据源</param>
        /// <param name="description">描述</param>
        /// <returns></returns>
        public TreeNode GetListMenu(int startInt, IEnumerable<TABC_JS_CostCenter> totalMenu, string description)
        {
            TreeNode tn = new TreeNode(description);
            FillData2TreeView(startInt, totalMenu, ref tn);
            return tn;
        }

        /// <summary>
        /// 前台页面获得菜单
        /// </summary>
        /// <param name="isShowDisabled">是否显示停用的</param>
        /// <returns></returns>
        public TreeNode GetListMenu(bool isShowDisabled)
        {
            List<TABC_JS_CostCenter> menu;
            if (isShowDisabled)
            {
                menu = (from c in db.TABC_JS_CostCenter
                        orderby c.作业中心ID ascending
                        select c).ToList();
            }
            else
            {
                menu = (from c in db.TABC_JS_CostCenter
                        where c.停用 == false
                        orderby c.作业中心ID ascending
                        select c).ToList();
            }
            return GetListMenu(1, menu, "作业中心");
        }


        /// <summary>
        /// Gets the list menu.
        /// </summary>
        /// <param name="startInt">The start int.</param>
        /// <param name="isShowDisabled">if set to <c>true</c> [is show disabled].</param>
        /// <returns></returns>
        public TreeNode GetListMenu(int startInt, bool isShowDisabled)
        {
            List<TABC_JS_CostCenter> menu;
            if (isShowDisabled)
            {
                menu = (from c in db.TABC_JS_CostCenter
                        orderby c.作业中心ID ascending
                        select c).ToList();
            }
            else
            {
                menu = (from c in db.TABC_JS_CostCenter
                        where c.停用 == false
                        orderby c.作业中心ID ascending
                        select c).ToList();
            }
            return GetListMenu(startInt, menu, "作业中心");
        }

        /// <summary>
        /// 插入作业中心
        /// </summary>
        /// <param name="costCenter"></param>
        /// <returns></returns>
        public int Insert(TABC_JS_CostCenter costCenter)
        {
            db.TABC_JS_CostCenter.InsertOnSubmit(costCenter);
            db.SubmitChanges();
            return 0;
        }

        /// <summary>
        /// 删除作业中心
        /// </summary>
        /// <param name="deptID"></param>
        /// <returns></returns>
        public int Delete(string deptID)
        {
            var cost = (from c in db.TABC_JS_CostCenter
                        where c.部门ID == deptID
                        select c).Single();
            db.TABC_JS_CostCenter.DeleteOnSubmit(cost);
            db.SubmitChanges();
            return 0;
        }

        /// <summary>
        /// 修改作业中心
        /// </summary>
        /// <param name="costCenter"></param>
        /// <returns></returns>
        public int Modify(TABC_JS_CostCenter costCenter)
        {
            var cost = (from c in db.TABC_JS_CostCenter
                        where c.作业中心ID == costCenter.作业中心ID
                        select c).Single();
            cost = costCenter;
            db.SubmitChanges();
            return 0;
        }


        /// <summary>
        /// 禁用作业中心
        /// </summary>
        /// <param name="costCenterID"></param>
        /// <param name="disable"></param>
        /// <returns></returns>
        public int SetDisable(int costCenterID, bool disable)
        {
            var cost = (from c in db.TABC_JS_CostCenter
                        where c.作业中心ID == costCenterID
                        select c).Single();
            cost.停用 = disable;
            try
            {
                db.SubmitChanges(ConflictMode.FailOnFirstConflict);
            }
            catch (Exception ex)
            {
                lastError = ex.Message;
                return -1;
            }
            return 0;
        }

        /// <summary>
        /// 根据作业中心ID获得作业中心一条记录
        /// </summary>
        /// <param name="costCenterID"></param>
        /// <returns></returns>
        public TABC_JS_CostCenter GetCostCenterByID(int costCenterID)
        {
            var cost = (from c in db.TABC_JS_CostCenter
                        where c.作业中心ID == costCenterID
                        select c).Single();
            return cost;
        }

        /// <summary>
        /// 根据作业中心ID返回作业中心名字
        /// </summary>
        /// <param name="costCenterID"></param>
        /// <returns></returns>
        public string GetCostCenterNameByID(int costCenterID)
        {
            return GetCostCenterByID(costCenterID).作业中心名称;
        }

        /// <summary>
        /// 返回TreeNode转变的ListItem[]结果
        /// </summary>
        /// <param name="tn">The tn.</param>
        /// <returns></returns>
        protected ListItem[] GetTreeNode2ListItem(TreeNode tn)
        {
            List<ListItem> li = new List<ListItem>();
            foreach (TreeNode tn1 in tn.ChildNodes)
            {
                if (tn1.ChildNodes.Count != 0)
                    li.AddRange(GetTreeNode2ListItem(tn1));
                else
                    li.Add(new ListItem(tn1.Text, tn1.Value));
            }
            return li.ToArray();
        }

        /// <summary>
        /// 获得ListItem数组形式的属于自己的作业中心
        /// </summary>
        /// <param name="costCenterID">The cost center ID.</param>
        /// <param name="isShowDisabled">if set to <c>true</c> [is show disabled].</param>
        /// <returns></returns>
        public ListItem[] GetMyCostCenters(int costCenterID, bool isShowDisabled)
        {
            return GetTreeNode2ListItem(GetListMenu(costCenterID, false));
        }

    }

}
